
Ext.namespace('pacp');


pacp.defectbuilder = new Ext.extend(Ext.Window, {
    title: 'Select an attribute',
    collapsible: false,
    maximizable: true,
    fieldStore: null,//list of field
    valueStore: null,//list of available values
    width: 500,
    height: 500,
    minWidth: 300,
    minHeight: 200,
    closeAction: 'hide',
    modal: true,
    layout: 'fit',
//    plain: true,
    //bodyStyle: 'padding:5px;',
    buttonAlign: 'center',
    //    renderTo: 'itemselector',
    initComponent: function(){
        this.items = this.buildWindow();//item selector form
        this.buttons = [this.buildButtons()];
        pacp.defectbuilder.superclass.initComponent.apply(this, arguments);
//        add custom event
                this.addEvents({
                    submitselection: true
                });
    },
    //	onRender: function(){
    //		pacp.multiselect.superclass.onRender.apply(this, arguments);
    //	},
    buildWindow: function(){
        var defectStore = new Ext.data.JsonStore({
			proxy: new Ext.data.HttpProxy({
				url: 'conditioncode.js',
				method: 'GET'
			}),
            root: 'data',
            fields: ['value', 'description']
			
        });
		
        var defectCombo = new Ext.form.ComboBox({
//			name: 'defect',
			allowBlank: false,
            store: defectStore,
            displayField: 'description',
			valueField: 'value',
			hiddenName: 'defect',
            typeAhead: true,
			fieldLabel: 'Defect',
            mode: 'remote',
			width: 300,
            forceSelection: true,
            triggerAction: 'all',
            emptyText: 'Select a defect...',
            selectOnFocus: true
        });
		
		var fieldStore = new Ext.data.JsonStore({
			proxy: new Ext.data.HttpProxy({
				url: 'defect_field_list.js',
				method: 'GET'
			}),
            root: 'data',
            fields: ['value']
			
        });
		var fieldCombo = new Ext.form.ComboBox({
			name: 'field',
			allowBlank: true,
            store: fieldStore,
            displayField: 'value',
            typeAhead: true,
			
			fieldLabel: 'Field',
            mode: 'remote',
			width:300,
            forceSelection: true,
            triggerAction: 'all',
            emptyText: 'Select a defect...',
            selectOnFocus: true
        });
        
		var operator = new Ext.form.RadioGroup({
			fieldLabel: 'Operator',
			allowBlank: true,
			name: 'op',
			items:[
				{boxLabel: '>', name: 'op', inputValue: '&gt;'},
				{boxLabel: '=', name: 'op', inputValue: '='},
				{boxLabel: '<', name: 'op', inputValue: '&lt;'},
				{boxLabel: ' like ', name: 'op', inputValue: ' like '}
			]
		});
		
		var valueStore = new Ext.data.JsonStore({
			proxy: new Ext.data.HttpProxy({
				url: 'defect_field_list.js',
				method: 'GET'
			}),
            root: 'data',
            fields: ['value']
        });
		
		
		var valueCombo = new Ext.form.ComboBox({
            store: valueStore,
			allowBlank: true,
            displayField: 'value',
            typeAhead: false,
			fieldLabel: 'Value',
			name: 'value',
            mode: 'remote',
			width: 300,
            forceSelection: false,
            triggerAction: 'all',
            emptyText: 'Select a value...',
            selectOnFocus: true
        });
		this.valueCombo = valueCombo;
		// modify the url for the action
		valueStore.on({
		    beforeload: {
		        fn: function (store, options) {
		            // use setUrl to change the URL for *just* this request.
		           	
		            store.proxy.setUrl('data/valuelist/' + options.params.field + '.js');
		        }
		    }
		});
		fieldCombo.on('change', function(ctl, nv, ov){
			valueStore.load({params:{field: nv}})
			
		});
		
		var operator2 = new Ext.form.RadioGroup({
			fieldLabel: '',
			allowBlank: true,
			name: 'op2',
			items:[
				{boxLabel: '>', name: 'op2', inputValue: '&gt;'},
				{boxLabel: '=', name: 'op2', inputValue: '='},
				{boxLabel: '<', name: 'op2', inputValue: '&lt;'}
			]
		});
        var qform = new Ext.form.FormPanel({
            title: '',
            width: this.width,
			frame: true,
			items: [{
				xtype: 'fieldset',
				autoHeight: true,
				items: [defectCombo, fieldCombo, operator, valueCombo]
			},{
				xtype: 'fieldset',
				autoHeight: true,
				items: [{xtype: 'label',  html: 'Do you want to count the number of the defect?<br>The number of defect is ...'},
				operator2, {xtype: 'numberfield', name: 'count'}]
			}]
            
            
        
        });
		this.qform = qform;
        return qform
        
        
    },
    
    buildButtons: function(){
        return new Ext.Button({
            text: 'OK',
            handler: function(){
//                var values = [];
//                Ext.each(this.isForm.getSelectedRecords(), function(r, index){
//                    values.push([r.get('value')])
//                });
//                Ext.Msg.alert('Submitted Values', 'The following will be sent to the server: <br />' + values.join(','));
              if (this.qform.getForm().isValid()){
                var values;
				values = this.qform.getForm().getValues();
				var text = values.defect;
				if (values.value!==this.valueCombo.emptyText){
					
					text = text + '[' + values.field + values.op + values.value + ']';
				}
				if (values.count) {
					text = text + values.op2 + values.count;
				}
				
				this.hide();
                //fire event
                this.fireEvent('submitselection', text, this);

			}
                
                
            },
            scope: this
        });
    }
    
});





